package com.toxictest.testproject.core;

import static playn.core.PlayN.*;

import java.util.ArrayList;
import java.util.List;

import playn.core.Game;
import playn.core.GroupLayer;
import playn.core.Image;
import playn.core.ImageLayer;
import playn.core.Pointer;
import java.util.Random;

public class TestGame implements Game {

	GroupLayer shapeLayer;
	List<AbstractShape> shapes = new ArrayList<AbstractShape>(0);
	AbstractShape currentShape;
	private float bottom;

	@Override
	public void init() {
		// create and add background image layer
		Image bgImage = assets().getImage("images/bg.png");
		ImageLayer bgLayer = graphics().createImageLayer(bgImage);
		graphics().rootLayer().add(bgLayer);

		shapeLayer = graphics().createGroupLayer();
		graphics().rootLayer().add(shapeLayer);

		// preload the pea image into the asset manager cache
		//assets().getImage(Shape.IMAGE);

		// add a listener for pointer (mouse, touch) input
		pointer().setListener(new Pointer.Adapter() {
			@Override
			public void onPointerEnd(Pointer.Event event) {
				
				if (currentShape==null) return;
				
				/*log().info("Click : x :" + event.x() + " ; y : " + event.y());
				log().info("Shape : x :" + currentShape.getLocationX() + " ; y : " + currentShape.getLocationY() );
				log().info("Shape size : x :" + currentShape.getWidth() + " ; y : " + currentShape.getHeight() );
				log().info("isShape : " + currentShape.isShape(event.x(), event.y() ));*/
				
				float leftSide = currentShape.getLocationX() - currentShape.getWidth()/2;
				float rightSide = currentShape.getLocationX() + currentShape.getWidth()/2;
				
				if ( currentShape.isShape(event.x(), event.y() )) currentShape.rotate();
				else {
					if ( event.x()< leftSide ) currentShape.moveLeft();
					if ( event.x()> rightSide ) currentShape.moveRight();
				}
				
				
			}
		});
		
		

	}

	@Override
	public void paint(float alpha) {
		// the background automatically paints itself, so no need to do anything
		// here!
	}

	@Override
	public void update(float delta) {
		
		boolean stop = false;
		
		if (currentShape!=null)  {
		
			if ( currentShape.getLocationY() >= bottom )  stop = true ;
			
			for (AbstractShape e1 : shapes) {
				
				if (e1==currentShape) break;
				
				for(int i=0; i < (int) currentShape.getBottomLine().length-1 ; i++){
					
					for(int j=0; j < (int) e1.getTopLine().length-1 ; j++){
						if (   (  ( (currentShape.getBottomLine()[i][0] >= e1.getTopLine()[j][0])
								&&
								(currentShape.getBottomLine()[i][0] <= e1.getTopLine()[j+1][0]) )
								||
								( (currentShape.getBottomLine()[i+1][0] >= e1.getTopLine()[j][0])
										&&
								(currentShape.getBottomLine()[i+1][0] <= e1.getTopLine()[j+1][0]) ) )
								&&
								( (currentShape.getBottomLine()[i][1] >= e1.getTopLine()[j][1])
								&&
								(currentShape.getBottomLine()[i+1][1] >= e1.getTopLine()[j][1])
								)
								) {
							stop=true;
						}
					}
					
				}
				
			}
			
		}
		else stop=true;
		

		if (!stop) currentShape.update();
		else {
			AbstractShape shape=null;
			Random randomGenerator = new Random();
			int obj = randomGenerator.nextInt(2);
			switch (obj) {
				case 0: shape = new Triangle(shapeLayer);
					
					break;
					
				case 1: shape = new Star(shapeLayer);
					
					break;
			}
			currentShape = shape;
			bottom = graphics().height() - currentShape.getHeight()/2 ;
			shapes.add(shape);
		}
		
		
	}

	@Override
	public int updateRate() {
		return 25;
	}
}
